/**
 * -- Validate 模块 --
 * 用于常用的正则验证
 */
export default function (str, rule) {
    let reg = new RegExp();
    switch (rule) {
        case 'phone':
            reg = /(^1[3|4|5|7|8|9]\d{9}$)|(^09\d{8}$)/;
            return reg.test(str)

        case 'idcard':
            reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
            if (!reg.test(str)) {
                return false;
            }
            //加权因子
            var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            //校验位
            var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
            var sum = 0;
            var ai = 0;
            var wi = 0;
            for (var i = 0; i < 17; i++) {
                ai = str[i];
                wi = factor[i];
                sum += ai * wi;
            }
            var last = parity[sum % 11];
            if (parity[sum % 11] != str[17]) {
                return false;
            }
            return true;

        case 'email':
            reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
            return reg.test(str);
    }
}